﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class AttendDutyDao
    {
        public bool Delete(SqlConnection conn, int dutyId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Attend_Duty] ");
            builder.Append("WHERE ");
            builder.Append("\t[DutyId] = @DutyId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DutyId", SqlDbType.Int) };
            commandParameters[0].Value = dutyId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public bool DeleteByDateTime(SqlConnection conn, DateTime beingDate, DateTime endDate)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Attend_Duty] ");
            builder.Append("WHERE ");
            builder.Append("\t[RegisterTime] >= @BeginDate ");
            builder.Append("\tAND [RegisterTime] <= @EndDate ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@BeginDate", SqlDbType.DateTime);
            commandParameters[0].Value = beingDate;
            commandParameters[1] = new SqlParameter("@EndDate", SqlDbType.DateTime);
            commandParameters[1].Value = endDate;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, AttendDutyData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Attend_Duty] ");
            builder.Append("( ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[RegisterType], ");
            builder.Append("\t[RegisterTime], ");
            builder.Append("\t[RegisterIp], ");
            builder.Append("\t[RegisterFlag], ");
            builder.Append("\t[Remark] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@UserId, ");
            builder.Append("\t@RegisterType, ");
            builder.Append("\t@RegisterTime, ");
            builder.Append("\t@RegisterIp, ");
            builder.Append("\t@RegisterFlag, ");
            builder.Append("\t@Remark ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[6];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = data.UserId;
            commandParameters[1] = new SqlParameter("@RegisterType", SqlDbType.Int);
            commandParameters[1].Value = data.RegisterType;
            commandParameters[2] = new SqlParameter("@RegisterTime", SqlDbType.DateTime);
            commandParameters[2].Value = data.RegisterTime;
            commandParameters[3] = new SqlParameter("@RegisterIp", SqlDbType.VarChar);
            commandParameters[3].Value = data.RegisterIp;
            commandParameters[4] = new SqlParameter("@RegisterFlag", SqlDbType.Int);
            commandParameters[4].Value = data.RegisterFlag;
            commandParameters[5] = new SqlParameter("@Remark", SqlDbType.NText);
            if (data.RemarkIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.Remark;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectByDutyId(SqlConnection conn, int dutyId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[DutyId], ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[RegisterType], ");
            builder.Append("\t[RegisterTime], ");
            builder.Append("\t[RegisterIp], ");
            builder.Append("\t[RegisterFlag], ");
            builder.Append("\t[Remark] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Attend_Duty] ");
            builder.Append("WHERE ");
            builder.Append("\t[DutyId] = @DutyId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DutyId", SqlDbType.Int) };
            commandParameters[0].Value = dutyId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Attend_Duty";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, int dutyId, string remark)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Attend_Duty] ");
            builder.Append("SET ");
            builder.Append("\t[Remark] = @Remark ");
            builder.Append("WHERE ");
            builder.Append("\t[DutyId] = @DutyId ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@DutyId", SqlDbType.Int);
            commandParameters[0].Value = dutyId;
            commandParameters[1] = new SqlParameter("@Remark", SqlDbType.NText);
            commandParameters[1].Value = remark;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

